<?php namespace Common\Model;
use Hdphp\Model\Model;
use Common\Model\GoodsAttr;
use Common\Model\Lnventory;
class Goods extends Model
{
    //指定商品表
    protected $table = 'goods';
    //指定自动验证信息
    protected $validate = array(
        //array(字段名，验证方法，错误信息，验证条件，验证事件）
        array('gname', 'required', '商品名称不能为空', 3, 3),
        array('category_cid', 'required', '分类名称不能为空', 3, 3),
        array('brand_bid', 'required', '品牌名称不能为空', 3, 3),
        array('unit', 'required', '商品名称不能为空', 3, 3),
        array('re_price', 'required', '原价名称不能为空', 3, 3),
        array('price', 'required', '销售价格名称不能为空', 3, 3)
    );
    protected $auto = array(
        array('upload_time', 'time', 'function', 3, 1),
        array('type_typeid','getTyid','method',3,3),
        array('admin_id','getAid','method',3,3)
    );


    //分类添加
    public function store()
    {
        if ($this->create()) {
            // P(Q('post.spec'));die;
            //获取商品id
            $gid=$this->add();
            //商品明细内容添加
            $lnventory=new \Common\Model\Lnventory;
            $lnventory->create();   
            //添加多张图片明细
            $pic=Q('post.picture');
            $pic=implode('|',$pic);
            $lnventory->data['picture']=$pic;
            $lnventory->data['goods_gid']=$gid;
//            P($lnventory->data);die;
            $lnventory->add();

           
            $goodsAttr=new GoodsAttr;
            //商品属性添加
            // p(Q('post.attr'));exit;
            foreach(Q('post.attr') as $taid=>$v){
                if($v){
                    $data=array(
                        'gavalue'=>$v,
                        'type_attr_taid'=>$taid,
                        'goods_gid'=>$gid
                    );
                    $goodsAttr->add($data);
                }
            }

            //商品规格添加
            $spec=Q('post.spec');
            if($spec){
                $value=$spec['value'];
                foreach($value as $k=>$v){
                    foreach($v as $kk=>$vv){
                        $data=array(
                        'gavalue'=>$vv,
                        'goods_gid'=>$gid,
                        'type_attr_taid'=>$k,
                        'extra_price'=>$spec['added'][$k][$kk]
                        );
                        $goodsAttr->add($data);
                    }
                }
            }

            
            return true;
        }
        return false;
    }
    //商品编辑
    /**
     * @return bool
     */
    public function edit(){
        if($this->create()){
            $gid=Q('post.gid',0,'intval');
            //如果有新上传的列表图片更改
            $gpath=$this->where("gid={$gid}")->pluck('listpic');
            if(Q('post.listpic')){
                // P(Q('post.picture'));die;
                    if($gpath!=Q('post.listpic')){
                        // echo 123;
                    unlink($gpath);
                }
            }else{
                if($gpath){
                    $this->data['listpic']=$gpath;
                }
            }
            $this->save();
            //处理商品详情数据
            $lnventory=new Lnventory();
//            $lpath=$lnventory->where("goods_gid={$gid}")->pluck('picture');
            $lid=$lnventory->where("goods_gid={$gid}")->pluck('lid');
            $lnventory->create();
            $pic=implode('|',$lnventory->data['picture']);
            $lnventory->data['picture']=$pic;
            $lnventory->data['goods_gid']=$gid;
            $lnventory->data['lid']=$lid;

            $lnventory->save();
            //保存商品属性表 添加之前全部删除原数据
            $goodsAttr=new GoodsAttr;
            // P(Q('post.attr'));die;
            $goodsAttr->where("goods_gid={$gid}")->delete();
             foreach(Q('post.attr') as $taid=>$v){
                if($v){
                foreach($v as $kk=>$vv){
                    
                    $data=array(
                        'gavalue'=>$vv,
                        'type_attr_taid'=>$taid,
                        'goods_gid'=>$gid
                    );
                    $goodsAttr->add($data);
                }
                }
                
            }

            //商品规格添加
            $spec=Q('post.spec');
            if($spec){
                $value=$spec['value'];
                foreach($value as $k=>$v){
                    foreach($v as $kk=>$vv){
                        $data=array(
                        'gavalue'=>$vv,
                        'goods_gid'=>$gid,
                        'type_attr_taid'=>$k,
                        'extra_price'=>$spec['added'][$k][$kk]
                        );
                        $goodsAttr->add($data);
                    }
                }
            }
            
            return true;

        }
        return false;
    }
    //自动获取类型id
    public function getTyid(){
        $cid=Q('post.category_cid',0,'intval');
        $type=new \Common\Model\Cate;
        return $type->where("cid={$cid}")->pluck('type_typeid');
    }
    //自动获取管理员id
    public function getAid(){
        return $_SESSION['info']['id'];
    }
}